/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addTienda.java
 *
 * Created on Apr 30, 2010, 11:26:09 AM
 */
package UI;

import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;
import Excepciones.TiendaException;
import robot_maker.*;
/**
 *
 * @author CarlosScipione
 */
public class addTienda extends javax.swing.JFrame {
     private boolean modify;
    private int idCod;
    private String Tname;

    /** Creates new form addTienda */
    public addTienda() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

    }

    UI_Tienda dueño;
    addTienda(UI_Tienda aThis, boolean mod, int id ) {
        initComponents();
        dueño = aThis;
        modify = mod;
        idCod = id;
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

         if(mod){

            Tienda datos = new Tienda();
            try {
                
                    datos = datos.getDatos(id);
                

            } catch (Exception ex) {
                Sistema.loggear(4, "CONTRUCTOR PANTALLA",
                        "NO SE PUDO TRAER DATOS DE LA BASE DE DATOS",
                        addCliente.class, "SQLException");
            }
            nombre_text.setText(datos.getNombre());
            calle_text.setText(datos.getCalle());
            Tname = datos.getNombre();
            estado_combo.setSelectedItem(datos.getEstado());
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cancelIt = new javax.swing.JButton();
        agregar = new javax.swing.JButton();
        calle_text = new javax.swing.JTextField();
        estado_combo = new javax.swing.JComboBox();
        estado_label = new javax.swing.JLabel();
        calle_label = new javax.swing.JLabel();
        title = new javax.swing.JLabel();
        nombre_text = new javax.swing.JTextField();
        nombre_label = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        cancelIt.setText("Cancelar");
        cancelIt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelItActionPerformed(evt);
            }
        });

        agregar.setText("Guardar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarActionPerformed(evt);
            }
        });

        estado_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Miranda", "Bolivar", "Apure", " " }));

        estado_label.setText("Estado:");

        calle_label.setText("Calle:");

        title.setText("Tienda:");

        nombre_label.setText("Nombre:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(title)
                    .add(nombre_label)
                    .add(layout.createSequentialGroup()
                        .add(3, 3, 3)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(89, 89, 89)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(layout.createSequentialGroup()
                                        .add(21, 21, 21)
                                        .add(cancelIt)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                        .add(agregar))
                                    .add(estado_combo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                            .add(estado_label)
                            .add(layout.createSequentialGroup()
                                .add(calle_label)
                                .add(54, 54, 54)
                                .add(calle_text, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(nombre_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 171, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(44, 44, 44)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .add(title)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nombre_label)
                    .add(nombre_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(estado_label)
                    .add(estado_combo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(calle_label)
                    .add(calle_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(agregar)
                    .add(cancelIt))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelItActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelItActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_cancelItActionPerformed

    private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed


        Tienda nueva = null;
        try {
            nueva = new Tienda(nombre_text.getText(),
                    estado_combo.getSelectedItem().toString(),
                    calle_text.getText());
        

                      if(modify){
                          nueva.setCodigo(idCod);
                        nueva.modificarBD(nueva, Tname);
                      }
                      else
                        nueva.ingresarBD(nueva);
                
            } catch (ProcesamientoDato ex) {
            }
             catch (TiendaException ex) {
            }
        this.setVisible(false);
        dueño.refrescar();
     
    }//GEN-LAST:event_agregarActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addTienda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregar;
    private javax.swing.JLabel calle_label;
    private javax.swing.JTextField calle_text;
    private javax.swing.JButton cancelIt;
    private javax.swing.JComboBox estado_combo;
    private javax.swing.JLabel estado_label;
    private javax.swing.JLabel nombre_label;
    private javax.swing.JTextField nombre_text;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables

}
